window.onload = function(){
	let reg1 = /^[a-z0-9_-]{3,16}$/;
	let reg2 = /^[a-z0-9_-]{6,18}$/;
	
	// 登录
	$("#LoginBtn1").on("click", function() {
		let flag = false;
		
		if (reg1.test($("#name1").val())) {
			if (reg2.test($("pass1").val())) {
				flag = true;
			} else {
				alert("密码格式错误")
			}
		} else {
			alert("用户名格式错误")
		}
		
		if(flag){
			$.ajax({
				type:"post",
				url:"./php/MyAccount.php",
				data:"name="+$("#name1").val()+"&pass="+$("#pass1").val(),
				success:function(msg){
					console.log(msg);
					if(msg==1){
						alert("登录成功！")
						location.href = "./index.html";
					}else{
						alert("用户名或密码错误！")
						location.reload();
					}
				}
			})		
		}
		
	})
	
	
	// 注册
	
	$("#LoginBtn2").on("click",function(){
		
		let flag = false;
		
		if(reg1.test($("#name2").val())){
			if(reg2.test($("#pass2").val())){
				flag = true;
			}else{
				alert("密码格式错误");
			}
		}else{
			alert("用户名格式错误");
		}
		
		if(flag){
			$.ajax({
				type:"post",
				url:"./php/selectuser.php",
				data:"name="+$("#name2").val(),
				success:function(msg){
					
					if(msg==1){
						alert("用户名存在")
					
					}else{
						$.ajax({
							type:"post",
							url:"./php/MyAccount_sign.php",
							data:"name="+$("#name2").val()+"&pass="+$("#pass2").val(),
							success:function(msg){
								
								if(msg==1){
									alert("注册成功")
									location.href= "./shoppingTrolley.html";
								}
							}
						})
					}
				}
			})
		}
	})
} 